/**
 * Copyright 2002 Cordys R&D B.V. 
 * 
 * This file is part of the Cordys .NET Connector. 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 //@Author	: Lejil 
//@Class	: CTSToNative

#include "stdafx.h"
#include "CTSToNative.h"

//
//	Creates SoapFault,if Any Exception occures in the .NET Call Handler
//
void CTSToNative::CreateSoapFault(int response,String *faultCode ,String *description,String *message)
{
	
	Document __gc *document			=	 Node::GetDocument(response);
	try
	{
		// The response will be SOAP:Fault
		Node::SetName(response,"SOAP:Fault");
		Node::RemoveAttribute(response, "xmlns");
		
		/*
		SOAP:Fault contains faultcode, faultstring, faultfactor and detail nodes
		<SOAP:Fault>
			<faultcode xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/">{1 ns0:Server}</faultcode>
			<faultstring></faultstring>
			<faultactor></faultactor>
			<detail></detail>
		<SOAP:Fault>
		*/
		
		// faultcode is "ns0:Server"
		int faultCodeNode			=   document->CreateTextElement("faultcode", "ns0:Server", response);
		Node::SetAttribute(faultCodeNode, "xmlns:ns0", "http://schemas.xmlsoap.org/soap/envelope/");		
		
		// faultstring is a message. In future, it has to be taken from the resource bundle.
		int faultStringNode	=	document->CreateTextElement("faultstring", description, response);
		Node::SetAttribute(faultStringNode, "xml:lang", "en-US");
		
		// faultfactor is the namespace
		document->CreateTextElement("faultactor", "http://schemas.cordys.com/1.0/metadata/dotnet", response);
 	
		// detail
		int detailNode		=	document->CreateElement("detail", response);
		
		
		/*
		detail contains cordys:FaultDetails, cordys:FaultRelatedException and ns1:customFaultDetail
		<detail>
			<cordys:FaultDetails xmlns:cordys="http://schemas.cordys.com/General/1.0/">
				<cordys:FaultDetailCode>{2Cordys.Integrator.Messages.FILE_NOT_FOUND}</cordys:FaultDetailCode>
				<cordys:FaultStringInsertion>{3MyFancyDocument}</cordys:FaultStringInsertion>
			</cordys:FaultDetails>
			<cordys:FaultRelatedException xmlns:cordys="http://schemas.cordys.com/General/1.0/">
				{5<![CDATA[java.net.SocketTimeoutException: Read timed out 
				at org.apache.axis.AxisFault.makeFault(AxisFault.java:129) 
			</cordys:FaultRelatedException>
			<ns1:customFaultDetail {6xmlns:ns1="http://schemas.cordys.com/MyComponent/1.0/"} xmlns:cordys="http://schemas.cordys.com/General/1.0/">
				<cordys:FaultDetailString>{7Operatie {8compress} mislukt}</cordys:FaultDetailString>
				<cordys:FaultDetailCode>{7Cordys.Integrator.Messages.PROCESSING_FAILED}</cordys:FaultDetailCode>
				<cordys:FaultStringInsertion>{8compress}</cordys:FaultStringInsertion>
			</ns1:customFaultDetail>
		</detail>
		*/
		
		// cordys:FaultDetails
		int faultDetailsNode	=	document->CreateElement("cordys:FaultDetails", detailNode);
		Node::SetAttribute(faultDetailsNode, "xmlns:cordys", "http://schemas.cordys.com/General/1.0/");
		
		//cordys:FaultDetailCode
		document->CreateTextElement("cordys:FaultDetailCode", "Cordys.DotNet.Messages.nativeException", faultDetailsNode);
		
		// Exception is not thrown. Has to be implemented
		
		//ns1:customFaultDetail
		int customFaultDetailNode	=	document->CreateElement("customFaultDetail", detailNode);
		Node::SetAttribute(customFaultDetailNode, "xmlns", "http://schemas.cordys.com/1.0/metadata/dotnet");
		Node::SetAttribute(customFaultDetailNode, "xmlns:cordys", "http://schemas.cordys.com/General/1.0/");
		
		//cordys:FaultDetailString
		document->CreateTextElement("cordys:FaultDetailString", message, customFaultDetailNode);
		
		//cordys:FaultDetailCode
		document->CreateTextElement("cordys:FaultDetailCode", faultCode, customFaultDetailNode);
		
	}
	catch(Exception *ex)
	{
		throw ex;
	}
}

